import CONSTANT from '@/common/constants.js';
import http from '@/common/http.js';

export default {
  name: 'aread',
  components: {},
  data () {
    return {
      siteId: '',
      studentId: '',
      openId: '',
      userName: '',
      userPass: '',
      idCardNeeds: false,
      placeholderText: '请输入学员姓名',
      areaList:[],
      showArea: false

    }
  },
  mounted () {
    let _this = this
    _this.oauth()
    let siteId = window.sessionStorage.getItem("siteId")
    http.post(CONSTANT.AREALIST, {siteId: siteId}, (res) => {
      console.log('quyu', res)
      if (res.length == 1) {
        window.sessionStorage.setItem("areaId",res[0].resCode)
        window.sessionStorage.setItem("areaName",res[0].resName)
        this.$router.push({path: '/Hello',query:{areaId:res[0].resCode,areaName:res[0].resName}})
      } else {
        _this.areaList = res
        _this.showArea = true
      }
    })

  },
  methods: {
    oauth() {
      //var s = 'http://qsngxm.hztangong.com/wx/index.html?siteId=0B6FC8D474F21CA6E0530100007F839B&wxAvatar=http://thirdwx.qlogo.cn/mmopen/vi_32/Q0j4TwGTfTLzWQgqxY3kMPxyWnNric9AgX4iayCB7TnRfibQs4sQ37RnU4m85vo5EbAWPDrB07XVSRltXibVOdibDBQ/132&appId=wxb7050c05fab552df&payOpen=1&studentId=27C53C7D71FB7DE0E0530100007F6947&studentName=%E9%BB%84%E7%8E%89%E6%9E%97&openId=oXZJV0sllFw5QfHXknvBBMMxZx5M#/area';
      var openId = window.sessionStorage.getItem('openId')
      if (openId == null || openId == '' || openId == 'undefined') {
        var s = window.location.href
        var arr = s.split('?')
        var url = arr[1]
        var theRequest = new Object()
        var strs = url.split('&')
        for (var i = 0; i < strs.length; i++) {
          //alert(strs[i])
          var content = strs[i].split('=')
          theRequest[content[0]] = content[1]
        }
        if (theRequest.openId == null) {
           location.href = "http://qsngxm.hztangong.com/wxOAuth?appId=" + theRequest.appId
        } else {
          var openIds = theRequest.openId.split('#/')
          window.sessionStorage.setItem('openId', openIds[0])
          window.sessionStorage.setItem('studentId', theRequest.studentId)
          window.sessionStorage.setItem('studentName', decodeURIComponent(theRequest.studentName))
          window.sessionStorage.setItem('siteId', theRequest.siteId)
          window.sessionStorage.setItem('wxAvatar', theRequest.wxAvatar)
          window.sessionStorage.setItem('appId', theRequest.appId)
          window.sessionStorage.setItem('payOpen', theRequest.payOpen)
          var currentUrl = window.location.href
          var targetUrl = currentUrl.replace(/openId=/, '')
          console.log(targetUrl.replace(/studentId=/, ''))
          window.location.href = targetUrl.replace(/studentId=/, '')
          //history.pushState(null,null,currentUrl)
          //alert(window.location.href)
        }
      }
    },
    goHello (id,name) {
      window.sessionStorage.setItem("areaId",id)
      window.sessionStorage.setItem("areaName",name)
      this.$router.push({path: '/Hello',query:{areaId:id,areaName:name}})
    },
    goBack () {
      window.history.go(-1)
    }
  }
}
